"""
string 的几种写法
1. 使用单引号 ''
2. 使用双引号 ""
"""


def testString():
    # 双引号 "" 的用法
    name = "Alice"

    # 单引号 '' 的用法
    address = 'Beijing China'

    print("isinstance(name, str) = {}".format(isinstance(name, str)))
    print("isinstance(address, str) = {}".format(isinstance(address, str)))

    # 使用转移字符 \
    singleStr = 'I am \'AI\''
    doubleStr  = "I am 'AI'"
    print("singleStr is {}".format(singleStr))
    print("doubleStr is {}".format(doubleStr))

    # 换行符 \n
    multilineStr = "First line\nSecond line"
    print("multilineStr is {}".format(multilineStr))

    # string是可以索引的，像数组一样
    # 索引从0到n
    # 索引也可以从n到0，但是索引就是-i
    word = "Python"
    print("word[0] is {}".format(word[0]))
    print("word[-2] is {}".format(word[-2]))


    # 切片操作 [0: n]
    print("word[:3] is {}".format(word[:3]))
    print("word[3:] is {}".format(word[3:]))
    print("word[-2:] is {}".format(word[-2:]))

    #-----------------------------------
    # 字符串的多行写法
    # 方法一 使用 """ ... """
    # 方法二 使用 ''' ... '''
    #-----------------------------------

    multilineStr = """\
        I
        am 
        OK
    """
    print("multlineStr is {}".format(multilineStr))

    multilineStr = '''\
        You
        are
        OK
    '''
    print("multilineStr is {}".format(multilineStr))

def testStrOpetator():
    # 连接符 +
    # 重复符 *
    print('3 * "un" + "num" is {}'.format(3 * "un" + "num"))

    # "Py" "thon"
    print('"Py" "thon" is {}'.format("Py" "thon"))

    # 分句操作
    text = (
        "i am ok"
        "are you fine?"
    )
    print(text)

def testStrFunction():
    # 去除空格 strip()
    # strip 去除任何空格
    # len 获取字符的长度
    helloWorld = "hello, World!"
    print(helloWorld)
    print("len of helloWorld is {}".format(len(helloWorld)))

    helloWorldSpace = "hello, Wrold! "
    print("len of hellpWorldSpace is {}".format(len(helloWorldSpace)))
    print("use strip func, str is {0}, and len is {1}".format(helloWorldSpace.strip(), len(helloWorldSpace.strip())))

    # lower() 和 upper() 方法
    print("str with lower() is {}".format(helloWorld.lower()))
    
    # replace()
    print("str use replect func is {}".format(helloWorld.replace('h', 'J')))

    # split(), 按照指定字符分割
    print("use split func, str is {}".format(helloWorld.split(',')))

    # capitalize(), 将第一个字符变为大写
    print("use capitalize, str is {}".format(helloWorld.capitalize()))

    # title(), 将每个单词的第一个字符大写
    print("use title, str is {}".format(helloWorld.title()))

    # join()
    name = ("Alice", "Alan", "Zee")
    print("', '.join(name) is {}".format(', '.join(name)))

    # isupper, 字符全部大写返回True
    print("'ABC'.isupper() is {}".format('ABC'.isupper()))
    print("'AbC'.isupper() is {}".format('AbC'.isupper()))

    # isalpha， 判断是否有字母组成
    print("'Company'.isalpha() is {}".format('Conpany'.isalpha()))

    # isdecimal, 若字符中全是数字就返回True
    print("'Con 12'.isdecimal() is {}".format('Con 12'.isdecimal()))
    print("'21324'.isdecimal() is {}".format('21324'.isdecimal()))

testStrFunction()
testStrOpetator()
testString()